<template>
  <div class="xtx-goods-page" v-if="goodDetail.categories">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem :to="`/category/${goodDetail.categories[1].id}`">{{
          goodDetail.categories[1].name
        }}</XtxBreadItem>
        <XtxBreadItem :to="`/category/sub/${goodDetail.categories[0].id}`">{{
          goodDetail.categories[0].name
        }}</XtxBreadItem>
        <XtxBreadItem>{{ goodDetail.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 图片预览区 -->
        <div class="media">
          <GoodsImage :pics="goodDetail.mainPictures" />
          <!-- 售卖 -->
          <GoodSales :goodDetail="goodDetail" />
        </div>
        <!-- 商品信息区 -->
        <div class="spec">
          <GoodName :goodDetail="goodDetail" />
          <!-- 商品的规格 -->
          <GoodsSku :goods="goodDetail" @change="getSku" />
          <XtxNumbox v-model="buyNum" :max="goodDetail.inventory" />
          <!-- 按钮 -->
          <XtxButton style="margin-top: 20px" type="primary" size="small">
            加入购物车
          </XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodTabs />
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside"></div>
      </div>
    </div>
  </div>
</template>

<script>
// 组件
import GoodsImage from './components/goods-image.vue'
import GoodSales from './components/goods-sales.vue'
import GoodName from './components/goods-name.vue'
import GoodTabs from './components/goods-tabs.vue'
import { findGoods } from '@/api/goods'
import { useRoute } from 'vue-router'
import { ref, onMounted, provide } from 'vue'
export default {
  components: {
    GoodsImage,
    GoodSales,
    GoodName,
    GoodTabs
  },
  setup () {
    // 购买的数量
    const buyNum = ref(1)
    const goodDetail = ref({})
    const route = useRoute()
    const getGoodDetail = async () => {
      const { result } = await findGoods(route.params.id)
      console.log('商品详情', result)
      goodDetail.value = result
    }
    // 注入商品详情数据
    provide('goodDetail', goodDetail)
    // 获取当前选择的sku数据
    /**
     * 有效sku包含两个有效信息
     * 1.库存
     * 2.价格
     */
    const getSku = (sku) => {
      console.log('有效的sku', sku)
      if (sku.skuId) {
        goodDetail.value.price = sku.price
        goodDetail.value.oldPrice = sku.oldPrice
        goodDetail.value.inventory = sku.inventory
      }
    }
    onMounted(() => {
      getGoodDetail()
    })
    return { goodDetail, getSku, buyNum }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  // 新增样式
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
    background: #fff;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
